<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>医生列表</title>
  <meta name="renderer" content="webkit" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
  <link rel="stylesheet" href="../../static/lib/layui/css/layui.css" media="all" />
  <link rel="stylesheet" href="../../static/css/admin.css" media="all" />
</head>

<body>
  <div id="TMM_app">
    <div class="layui-fluid">
      <div class="layui-card">
        <div class="layui-tab layui-tab-brief tmm-table-tab" lay-filter="component-tabs-brief">
<%--          <ul class="layui-tab-title" id="ulStatus">--%>
<%--            <li status="-1" class="layui-this" id="allGoods"></li>--%>
<%--            <li status="1" id="upGoods"></li>--%>
<%--            <li status="0" id="downGoods"></li>--%>
<%--            <li status="2" id="waitExamine"></li>--%>
<%--          </ul>--%>
          <div class="layui-tab-content">
          </div>
        </div>
        <div class="layui-form layui-card-header tmm-admin-card-header-auto tmm-filter-form">
          <div class="layui-form-item">
            <input type="hidden" id="statusInput"/>
            <div class="layui-inline">
              <label class="layui-form-label" style="width: 70px;">输入搜索</label>
              <div class="layui-input-inline">
                <input type="text" name="name" id="name" placeholder="商品名称" autocomplete="off" class="layui-input" />
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label" style="width: 70px;">商品类目</label>
              <div class="layui-input-inline">
                <select name="categoryPid" id="categoryPid" lay-filter="categoryPid">
                  <option value="">请选择产品类目</option>
                  <c:forEach items="${categoryList}" var="category">
                    <option value="${category.id}">${category.name}</option>
                  </c:forEach>
                </select>
              </div>
            </div>
            <div class="layui-inline">
              <button class="layui-btn tmm-admin-btn-list" id="query" lay-submit="" lay-filter="LAY-app-contlist-search">
                搜索
              </button>
            </div>
          </div>
        </div>
        <div class="layui-card-body">
          <div class="tmm-head-button">
            <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px; text-align: right;">
              <button class="layui-btn tmm-admin-btn-tags" lay-href="${path}/goods/add">
                添加
              </button>
            </div>
          </div>

          <table id="tmm-goods-table" lay-filter="tmm-doctors" class="layui-table"></table>
        </div>
      </div>
    </div>
  </div>

  <script src="../../static/lib/layui/layui.js"></script>
  <script src="../../static/js/jquery.min.js"></script>
  <script>
    layui
      .config({
        base: "../../static/js/"
      })
      .extend({
        index: "index"
      })
      .use(["index", "table","laydate"], function () {
        var $ = layui.$,
          admin = layui.admin,
          view = layui.view,
          table = layui.table,
          laydate = layui.laydate,
          form = layui.form;

        table.render({
          elem: "#tmm-goods-table",
          skin: 'nob',
          size: 'lg',
          height: 'full-280',
          even: true,
          page: true,
          limit: 10,
          limits: [10, 20, 50],
          url: "${path}/goods/getList",
          where: {
              name:$("#name").val(),
              categoryPid:$("#categoryPid").val(),
          },
          cols: [[
              {field:'cover_pic', title: '商品logo',width:'10%',templet:function (d) {
                  var html ='暂无图片';
                  if(d.cover_pic!=null && d.cover_pic!=''){
                      html = '<img src="'+d.cover_pic+'" style="height: 50px;"/>';
                  }
                  return html;
              }},
              {field:'categoryName', title: '类目',width:'7%',templet:function (d) {
                  var categoryName = "";
                      $("#categoryPid option").each(function(){
                          if($(this).val()==d.category_pid){
                              categoryName = $(this).text();
                              return;
                          }
                      });
                      return categoryName;
                  }},

              {field:'name', title: '商品名称',width:'10%'},


              {field:'units', title: '商品规格',width:'7%',templet:function (d) {
                  return d.units;
              }},
              {field:'price', title: '商品价格',width:'10%'},
              {field:'is_recommend', title: '是否置顶',width:'10%',templet:function (d) {
                  if(d.is_recommend==2){
                    return '否';
                  }else if(d.is_recommend==1){
                    return '是';
                  }else{
                    return d.is_recommend;
                  }
              }},
              {field:'inventory', title: '库存',width:'10%'},
              {field:'idx', title: '排序',width:'10%'},
              {field:'status', title: '状态',width:'10%',templet:function (d) {
                  if(d.status==2){
                    return '下架';
                  }else if(d.status==1){
                    return '上架';
                  }else{
                    return d.status;
                  }
              }},
              {field:'id', title: '操作',templet:function (d) {
                  var html = '';
                  html +=  '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="addInventory">商品入库</a>';
                  html +=  '<a class="layui-btn layui-btn-normal layui-btn-xs" href="${path}/goods/edit?id='+d.id+'" lay-event="edit">修改</a>';
                  if(d.status==2){
                      html +=  '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="up">上架</a>';
                  }else if(d.status==1){
                      html +=  '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="down">下架</a>';
                  }
                  return html;
              }},
          ]],
            text:{none:"暂无数据"},
          done: function(res, curr, count){
          }
        });
        table.on("tool(tmm-doctors)", function (t) {
          var n = t.data;
          switch (t.event){
              //商品入库
              case 'addInventory':
                var inventoryAlter = layer.open({
                  type: 1,
                  title: '商品入库',
                  skin: 'layui-layer-rim',
                  area: ['60%', '50%'],
                  shadeClose: true,
                  content: '\
						<div class="layui-card-body">\
						<form class="layui-form" action="" lay-filter="form_editActiveItem"><div class="layui-row layui-col-space10 layui-form-item">\
							有效期：<input type="text" name="validity" id="validity" lay-verify="required" placeholder="请选择时间" autocomplete="off" class="layui-input" />\
							库存：<input type="text" name="inventory" id="inventory" lay-verify="required" placeholder="请输入库存" autocomplete="off" class="layui-input" />\
							<div class="layui-form-item">\
								<div class="layui-input-block" style="text-align:center;margin: 0">\
									<button class="layui-btn" type="button" lay-submit lay-filter="inventorySub">提交</button>\
								</div>\
							</div>\
						</div>\
						</form>\
						</div>'
                });

                laydate.render({
                  elem: "#validity",
                  trigger: 'click',
                  theme: '#d6b284',
                  btns:['clear']
                });

                form.on('submit(inventorySub)', function(obj){
                  var index2 = layer.load(2);
                  $.ajax({
                    url:"${path}/goods/addInventory"
                    ,type:'post'
                    ,data:{goods_id:n.id,validity:$("#validity").val(),inventory:$("#inventory").val()}
                    ,dataType: "json"
                    ,success:function(ret){
                      if(ret.code != 0){
                        layer.close(index2);
                        layer.msg(ret.msg);
                        return;
                      }
                      layer.msg(ret.msg, {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1500
                      }, function(){
                        layer.close(index2);
                        layer.close(inventoryAlter);
                        //执行重载
                        table.reload('tmm-goods-table', {
                          page: {
                            curr:$(".layui-laypage-em").next().html(),
                          }
                          ,where: {
                            name:$("#name").val(),
                            categoryPid:$("#categoryPid").val(),
                            categoryId:$("#categoryId").val(),
                          }
                        }, 'data');
                      });

                    }
                    ,error: function () {
                      layer.close(index2);
                    }
                  });
                  return false;
                });
              return;
              //加入秒杀
              case 'addSeckill':
                var seckillAlter = layer.open({
                  type: 1,
                  title: '加入秒杀',
                  skin: 'layui-layer-rim',
                  area: ['60%', '50%'],
                  shadeClose: true,
                  content: '\
						<div class="layui-card-body">\
						<form class="layui-form" id="seckillForm"><div class="layui-row layui-col-space10 layui-form-item">\
						    <input type="hidden" name="goods_id" id="goods_id" value="'+n.id+'"/>\
							有效期：<select name="time" id="time" multiple lay-verify="required">\
                                    <option value="" selected="selected">全部</option>\
                                  </select>\
							规格：<select name="unit" id="unit" multiple lay-verify="required">\
                                    <option value="" selected="selected">全部</option>\
                                  </select>\
							<div class="layui-form-item">\
								<div class="layui-input-block" style="text-align:center;margin: 0">\
									<button class="layui-btn" type="button" lay-submit lay-filter="seckillSub">提交</button>\
								</div>\
							</div>\
						</div>\
						</form>\
						</div>'
                });

                //填充规格数据
                var units = n.units.split(',');
                for(var k=0;k<units.length;k++){
                  $("#unit").append('<option value="'+units[k]+'">'+units[k]+'</option>')
                }

                //填充有效期数据
                $.ajax({
                  url:"${path}/goods/getSeckillTime"
                  ,type:'get'
                  ,data:{goods_id:n.id}
                  ,dataType: "json"
                  ,success:function(ret){
                    if(ret.code != 0){
                      layer.msg(ret.msg);
                      return;
                    }
                    var times = ret.data;
                    for(var k=0;k<times.length;k++){
                      $("#time").append('<option value="'+times[k]+'">'+times[k]+'</option>')
                    }
                    form.render();
                  }
                });

                form.render();

                laydate.render({
                  elem: "#seckill_time",
                  trigger: 'click',
                  theme: '#d6b284',
                  btns:['clear']
                });

                form.on('submit(seckillSub)', function(obj){
                  var index2 = layer.load(2);
                  $.ajax({
                    url:"${path}/goods/addSeckill"
                    ,type:'post'
                    ,data:$("#seckillForm").serialize()
                    ,dataType: "json"
                    ,success:function(ret){
                      if(ret.code != 0){
                        layer.close(index2);
                        layer.msg(ret.msg);
                        return;
                      }
                      layer.msg(ret.msg, {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1500
                      }, function(){
                        layer.close(index2);
                        layer.close(seckillAlter);
                        //执行重载
                        table.reload('tmm-goods-table', {
                          page: {
                            curr:$(".layui-laypage-em").next().html(),
                          }
                          ,where: {
                            name:$("#name").val(),
                            categoryPid:$("#categoryPid").val(),
                            categoryId:$("#categoryId").val(),
                          }
                        }, 'data');
                      });

                    }
                    ,error: function () {
                      layer.close(index2);
                    }
                  });
                  return false;
                });
                return;
              return;
              //上架产品
              case 'up':
                  layer.confirm("确定上架产品", function (e) {
                      var index = layer.load(2);
                      $.ajax({
                          type:"post",
                          url:"${path}/goods/shelf",
                          dataType:"json",
                          data:{id:n.id,shelf:1},
                          success:function(data){
                              layer.close(index);
                              if(data.code ==0){
                                  layer.alert(data.msg,{icon:1}, function(){
                                      location.reload();
                                  });
                              }else{
                                  layer.alert(data.msg,{icon:2});
                              }
                          },
                          error:function (data) {
                              layer.close(index);
                          }
                      });
                      return false;
                  })
                  return;
              //下架产品
              case 'down':
                  layer.confirm("确定下架产品", function (e) {
                      var index = layer.load(2);
                      $.ajax({
                          type:"post",
                          url:"${path}/goods/shelf",
                          dataType:"json",
                          data:{id:n.id,shelf:2},
                          success:function(data){
                              layer.close(index);
                              if(data.code ==0){
                                  layer.alert(data.msg,{icon:1}, function(){
                                      location.reload();
                                  });
                              }else{
                                  layer.alert(data.msg,{icon:2});
                              }
                          },
                          error:function (data) {
                              layer.close(index);
                          }
                      });
                      return false;
                  })
                  return;
          }
        });
        //查询
        $('#query').on('click', function(){
            table.reload('tmm-goods-table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    name:$("#name").val(),
                    categoryPid:$("#categoryPid").val(),
                    categoryId:$("#categoryId").val(),
                }
            }, 'data');
        });
        //状态页签查询查询
        $('#ulStatus li').on('click', function(){
            $("#statusInput").val($(this).attr("status"));
            table.reload('tmm-goods-table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    name:$("#name").val(),
                    categoryPid:$("#categoryPid").val(),
                    categoryId:$("#categoryId").val(),
                    status:$(this).attr("status")
                }
            }, 'data');
        });
      });
  </script>
</body>

</html>